package com.example.expresshelp.service;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.example.expresshelp.bean.Address;
import com.example.expresshelp.bean.City;
import com.example.expresshelp.bean.Province;
import com.example.expresshelp.db.MySqliteHelper;
import com.example.expresshelp.util.Constance;
import com.example.expresshelp.util.Validator;

public class ProvinceService {
	private static final String TAG = "ExpressService";

	/**
	 * 解析省份信息
	 * 
	 * @param province
	 */
	public static List<Province> parseProvince(Context context,
			String provinceHtml) {
		if (!Validator.isEffective(provinceHtml)) {
			return null;
		}
		List<Province> provinceList = new ArrayList<Province>();
		Document document = Jsoup.parse(provinceHtml);
		Elements provinceRows = document.getElementsByClass("provincetr"); // 行数
		// todo 只解析北京数据
		for (int i = 0; i < provinceRows.size(); i++) {
			Element provinceRow = provinceRows.get(i);
			Elements tds = provinceRow.getElementsByTag("td");
			for (int j = 0; j < tds.size(); j++) { // 一行个数
				Province province = new Province();
				Element td = tds.get(j);
				Element a = td.getElementsByTag("a").get(0);
				String cityHtml = a.attr("href");
				String provinceCode = cityHtml.split(".html")[0];
				String provinceName = a.childNodes().get(0).toString();
				Log.i("express", "provinceName - >" + provinceCode
						+ provinceName);
				long pid = saveToDB(context, provinceCode, provinceName);
				List<City> cityList = CityService.parseCity(context, pid + "",
						provinceCode, cityHtml);
				province.setId(pid + "");
				province.setCode(provinceCode);
				province.setName(provinceName);
				province.setCityList(cityList);
				provinceList.add(province);
			}
		}
		return provinceList;

	}

	/**
	 * 解析省份信息
	 * 
	 * @param province
	 */
	public static List<Province> parseProvince2(Context context,
			String provinceHtml, int r, int c) {
		if (!Validator.isEffective(provinceHtml)) {
			return null;
		}
		List<Province> provinceList = new ArrayList<Province>();
		Document document = Jsoup.parse(provinceHtml);
		Elements provinceRows = document.getElementsByClass("provincetr"); // 行数
		// todo 只解析北京数据
		Element provinceRow = provinceRows.get(r);
		Elements tds = provinceRow.getElementsByTag("td");
		Province province = new Province();
		Element td = tds.get(c);
		Element a = td.getElementsByTag("a").get(0);
		String cityHtml = a.attr("href");
		String provinceCode = cityHtml.split(".html")[0];
		String provinceName = a.childNodes().get(0).toString();
		Log.i("express", "provinceName - >" + provinceCode + provinceName);
		long pid = saveToDB(context, provinceCode, provinceName);
		List<City> cityList = CityService.parseCity(context, pid + "",
				provinceCode, cityHtml);
		province.setId(pid + "");
		province.setCode(provinceCode);
		province.setName(provinceName);
		province.setCityList(cityList);
		provinceList.add(province);
		return provinceList;

	}

	/**
	 * 得到所有的省份信息
	 * 
	 * @return
	 */
	public static List<Province> getAllProvince(Context context) {
		List<Province> provinceList = null;
		String where = " 1=1 ";
		Cursor cursor = MySqliteHelper.getInstance(context).query(
				Constance.PROVINCE_TABLE_NMAE, where);
		if (cursor != null) {
			provinceList = new ArrayList<Province>();
			while (cursor.moveToNext()) {
				Province province = new Province();
				province.setCode(cursor.getString(cursor.getColumnIndex("code")));
				province.setName(cursor.getString(cursor.getColumnIndex("name")));
				provinceList.add(province);
			}
		}
		return provinceList;
	}

	/**
	 * 把省份信息保存到数据库中
	 * 
	 * @param context
	 * @param code
	 * @param name
	 */
	public static long saveToDB(Context context, String code, String name) {
		try {
			// 构造参数
			ContentValues values = new ContentValues();
			values.put("code", code);
			values.put("name", name);
			return MySqliteHelper.getInstance(context).insert(
					Constance.PROVINCE_TABLE_NMAE, null, values);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
	}

	/**
	 * 
	 * @param context
	 * @param code
	 * @return
	 */
	public static Province find(Context context, String pid) {
		Province province = null;
		String where = " id='" + pid + "'";
		Cursor cursor = MySqliteHelper.getInstance(context).query(
				Constance.PROVINCE_TABLE_NMAE, where);
		if (cursor != null) {
			while (cursor.moveToNext()) {
				province = new Province();
				province.setCode(cursor.getString(cursor.getColumnIndex("code")));
				province.setName(cursor.getString(cursor.getColumnIndex("name")));
			}
		}
		return province;
	}
	
	/**
	 * 
	 * @param context
	 * @param code
	 * @return
	 */
	public static List<Address> getProvinceByName(Context context, String name) {
		if ((name.equals("市辖区") || name.equals("县"))) {
			return null;
		}
		List<Address> addressList = null;
		List<Province> list = null;
		String where = " name like '%" + name + "%'";
		Cursor cursor = MySqliteHelper.getInstance(context).query(
				Constance.PROVINCE_TABLE_NMAE, where);
		if (cursor != null) {
			list = new ArrayList<Province>();
			addressList = new ArrayList<Address>();
			while (cursor.moveToNext()) {
				Province province = new Province();
				Address address = new Address();
				province.setCode(cursor.getString(cursor.getColumnIndex("code")));
				province.setName(cursor.getString(cursor.getColumnIndex("name")));
				list.add(province);
				address.setFilterAddress(province.getName());
				addressList.add(address);
			}
		}
		return addressList;
	}
}
